package com.bnz.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.bnz.pojo.Classes;
import com.bnz.pojo.Student;
import com.bnz.service.ClassesService;
import com.bnz.service.StudentService;
import com.bnz.util.PageResult;
import com.bnz.vo.StudentVo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
@Controller
@RequestMapping("/student")
public class StudentController {
	@Reference
	private StudentService studentService;
	@Reference
	private ClassesService classesService;
	//1. 查看学生列表
	@RequestMapping("list")
	public String findAll(Model model){
		//1.1 得到学生列表
		List<Student> students = studentService.findAll();
		//1.2 将上面查询到的数据放到model中
		model.addAttribute("students",students);
		//1.3 查询所有班级
		List<Classes> classes = classesService.findAll();
		//1.4 将所有的班级放到model中
		model.addAttribute("classes",classes);
		//1.5 返回逻辑视图名
		return "/student/list";
	}

	//2. 添加学生
	@RequestMapping("add")
	public String add(Student stud){
		//2.1 添加到数据库中
		studentService.add(stud);
		System.out.println("stud = " + stud);
		return "redirect:/student/list.do";
	}

	//3. 修改学生
	@RequestMapping("update")
	public String update(Student stud){
		//2.1 修改到数据库中
		studentService.update(stud);
		System.out.println("stud = " + stud);
		return "redirect:/student/list.do";
	}

	//4. 删除学生
	@RequestMapping("delete")
	public String delete(Integer sid){
		studentService.delete(sid);
		return "redirect:/student/list.do";
	}

	//5. 条件查询带分页
	@RequestMapping("search")
	public String search(@RequestParam(defaultValue = "1",required = false) Integer page,
	                     @RequestParam(defaultValue = "5",required = false) Integer pagesize,
	                     StudentVo vo,
	                     HttpServletRequest request, HttpServletResponse response,
	                     HttpSession session, Model model){
	                     // Model model){
		System.out.println("search....");
		//5.1 得到查询结果
		PageResult<Student> pr = studentService.search(page, pagesize, vo);
		//5.2 将pr放到map中
		model.addAttribute("pr",pr);
		model.addAttribute("vo",vo);
		//5.3 查询所有班级
		model.addAttribute("classes",classesService.findAll());
		//5.3 返回视图
		return "/student/list";
	}

	@RequestMapping("hello")
	@ResponseBody
	public void sayHello(){
		System.out.println("sayHello...");
	}
}
